// ==UserScript==
// @name           IkariamEnhancedMail
// @namespace      IkariamEnhancedMail
// @description    Add BBCode to Ikariam Mail
// @include        http://s*.ikariam.tld/index.php?view=sendIKMessage*
// @include        http://s*.ikariam.tld/index.php?view=diplomacyAdvisor*
// @include        http://s*.ikariam.tld/index.php?view=diplomacyAdvisorOutBox*

// @require        http://devden.googlecode.com/svn/trunk/support/jquery.js
// @require        http://devden.googlecode.com/svn/trunk/support/support.js

// @resource       bold       http://devden.googlecode.com/svn/trunk/ikariamenhancedmail/images/bold.png
// @resource       italic     http://devden.googlecode.com/svn/trunk/ikariamenhancedmail/images/italic.png
// @resource       underline  http://devden.googlecode.com/svn/trunk/ikariamenhancedmail/images/underline.png
// @resource       link       http://devden.googlecode.com/svn/trunk/ikariamenhancedmail/images/link.png
// @resource       picture    http://devden.googlecode.com/svn/trunk/ikariamenhancedmail/images/picture.png
// @resource       bg_message http://devden.googlecode.com/svn/trunk/ikariamenhancedmail/images/bg_message.gif

var textarea;
var content;

function init() {
    var view = urlParse("view");
    var action = urlParse("action");
    switch (view) {
        case "sendIKMessage":
            initsendIKMessage();
            break;
        case "diplomacyAdvisor":
            initdiplomacyAdvisor();
            break;
        case "diplomacyAdvisorOutBox":
            initdiplomacyAdvisor();
            break;
    }
}



function initdiplomacyAdvisor() {
    $(".msgText").each(function(i){
        //GM_log("Converting: "+BBC2HTML($(this).html()));
        $(this).html(BBC2HTML($(this).html()));
    });

}
//transparent url(layout/bg_message.gif) no-repeat scroll 0 0
function initsendIKMessage() {
    // Extend the background...
    $("#notice").css('background', 'transparent url("'+GM_getResourceURL("bg_message")+'")  no-repeat scroll 0 0');
    // Add BBCode
    var tbcode = '<div id="ikariamenhancedbbcode">';
    tbcode += '<img class="button" src="'+GM_getResourceURL("bold")+'" name="btnBold" id="btnBold">';
    tbcode += '<img class="button" src="'+GM_getResourceURL("italic")+'" name="btnItalic" id="btnItalic">';
    tbcode += '<img class="button" src="'+GM_getResourceURL("underline")+'" name="btnUnderline" id="btnUnderline">';
    tbcode += '<img class="button" src="'+GM_getResourceURL("link")+'" name="btnLink" id="btnLink">';
    tbcode += '<img class="button" src="'+GM_getResourceURL("picture")+'" name="btnPicture" id="btnPicture">';
    tbcode += '</div>';
    var textareahtml = $("#text").html
    $("#text").before(tbcode);
    //$("#ikariamenhancedbbcode").show();
	
    $("#btnBold").click(function () {
        doAddTags("[b]", "[/b]", "text");
    });
    $("#btnItalic").click(function () {
        doAddTags("[i]", "[/i]", "text");
    });
    $("#btnUnderline").click(function () {
        doAddTags("[u]", "[/u]", "text");
    });
    $("#btnLink").click(function () {
        doURL("text");
    });
    $("#btnPicture").click(function () {
        doImage("text");
    });
	
	
	
	
}

//blah


function doImage(obj) {
    textarea = document.getElementById(obj);
    var url = prompt('Enter the Image URL:','http://');
    var scrollTop = textarea.scrollTop;
    var scrollLeft = textarea.scrollLeft;
    if (document.selection) {
        textarea.focus();
        var sel = document.selection.createRange();
        sel.text = '[img]' + url + '[/img]';
    } else {
        var len = textarea.value.length;
        var start = textarea.selectionStart;
        var end = textarea.selectionEnd;
        var sel = textarea.value.substring(start, end);
        var rep = '[img]' + url + '[/img]';
        textarea.value =  textarea.value.substring(0,start) + rep + textarea.value.substring(end,len);
        textarea.scrollTop = scrollTop;
        textarea.scrollLeft = scrollLeft;
    }

}

function doURL(obj) {
    textarea = document.getElementById(obj);
    var url = prompt('Enter the URL:','http://');
    var scrollTop = textarea.scrollTop;
    var scrollLeft = textarea.scrollLeft;
    if (document.selection) {
        textarea.focus();
        var sel = document.selection.createRange();
        if(sel.text==""){
            sel.text = '[url]'  + url + '[/url]';
        } else {
            sel.text = '[url=' + url + ']' + sel.text + '[/url]';
        }
    } else {
        var len = textarea.value.length;
        var start = textarea.selectionStart;
        var end = textarea.selectionEnd;
        var sel = textarea.value.substring(start, end);
        if(sel==""){
            var rep = '[url]' + url + '[/url]';
        } else {
            var rep = '[url=' + url + ']' + sel + '[/url]';
        }
        textarea.value =  textarea.value.substring(0,start) + rep + textarea.value.substring(end,len);
        textarea.scrollTop = scrollTop;
        textarea.scrollLeft = scrollLeft;
    }
}

function doAddTags(tag1,tag2,obj) {
    textarea = document.getElementById(obj);
    if (document.selection) {
        textarea.focus();
        var sel = document.selection.createRange();
        sel.text = tag1 + sel.text + tag2;
    } else {
        var len = textarea.value.length;
        var start = textarea.selectionStart;
        var end = textarea.selectionEnd;
        var scrollTop = textarea.scrollTop;
        var scrollLeft = textarea.scrollLeft;
        var sel = textarea.value.substring(start, end);
        var rep = tag1 + sel + tag2;
        textarea.value =  textarea.value.substring(0,start) + rep + textarea.value.substring(end,len);
        textarea.scrollTop = scrollTop;
        textarea.scrollLeft = scrollLeft;
    }
}

function doList(tag1,tag2,obj){
    textarea = document.getElementById(obj);
    if (document.selection) {
        textarea.focus();
        var sel = document.selection.createRange();
        var list = sel.text.split('\n');
        for(i=0;i<list.length;i++) {
            list[i] = '[*]' + list[i];
        }
        sel.text = tag1 + '\n' + list.join("\n") + '\n' + tag2;
    } else {
        var len = textarea.value.length;
        var start = textarea.selectionStart;
        var end = textarea.selectionEnd;
        var i;
        var scrollTop = textarea.scrollTop;
        var scrollLeft = textarea.scrollLeft;
        var sel = textarea.value.substring(start, end);
        var list = sel.split('\n');
        for(i=0;i<list.length;i++) {
            list[i] = '[*]' + list[i];
        }
        var rep = tag1 + '\n' + list.join("\n") + '\n' +tag2;
        textarea.value =  textarea.value.substring(0,start) + rep + textarea.value.substring(end,len);
        textarea.scrollTop = scrollTop;
        textarea.scrollLeft = scrollLeft;
    }
}




function BBC2HTML(s) {
	return s.replace(/\[img\]((?:http|ftp|https):\/\/[\w-_]+(?:\.[\w-_]+)+(?:[\w-\.,@?^=%&amp;:/~\+#]*[\w-\@?^=%&amp;/~\+#]))\[\/img]/gi,'<img src="$1" />')
		.replace(/\[quote]/gi,'<div style="background-color:#F0F0F0;"><blockquote>')
		.replace(/\[quote="([\w\s]*)"\]/gi, '<div style="color:#006600;font-weight:bold;">$1 wrote :</div><div style="background-color:#F0F0F0;"><blockquote>')
		.replace(/\[\/quote]/gi,'</blockquote></div>')
		.replace(/\[code]/gi,'<div style="color:#006600;font-weight:bold;">Code :</div><div style="color:#333333;background-color:#F0F0F0;"><code>')
		.replace(/\[\/code]/gi,'</code></div>')
		.replace(/\[b]/gi,'<strong>')
		.replace(/\[\/b]/gi,'</strong>')
		.replace(/\[center]/gi,'<center>')
		.replace(/\[\/center]/gi,'</center>')
		.replace(/\[i]/gi,'<i>')
		.replace(/\[\/i]/gi,'</i>')
		.replace(/\[u]/gi,'<u>')
		.replace(/\[\/u]/gi,'</u>')
		.replace(/\[strike]/gi,'<strike>')
		.replace(/\[\/strike]/gi,'</strike>')
		.replace(/\[color=(#[a-f0-9]{6}|#[a-f0-9]{3}|\w*)\]/gi, '<font color="$1">')
		.replace(/\[\/color]/gi,'</font>')
		.replace(/\[size=(\d*?)\]/gi, '<div style="font-size:$1px;line-height:normal;">')
		.replace(/\[\/size]/gi,'</div>')
		.replace(/\[align=(\w*?)\]/gi, '<div align="$1">')
		.replace(/\[\/align]/gi,'</div>')
		.replace(/\[list\](.*?)\[\/list\]/gi, '<ul>$1</ul>')
		.replace(/\[list=1\](.*?)\[\/list\]/gi, '<ol>$1</ol>')
		.replace(/\[list=a\](.*?)\[\/list\]/gi, '<ol type="a">$1</ol>')
		.replace(/\[\*]/gi,'<li>')
		.replace(/\[url]((?:http|ftp|https):\/\/[\w-_]+(?:\.[\w-_]+)+(?:[\w-\.,@?^=%&amp;:/~\+#]*[\w-\@?^=%&amp;/~\+#]))\[\/url]/gi, '<a href="$1" target="_blank">$1</a>' )
		.replace(/\[url=((?:http|ftp|https):\/\/[\w-_]+(?:\.[\w-_]+)+(?:[\w-\.,@?^=%&amp;:/~\+#]*[\w-\@?^=%&amp;/~\+#]))\]/gi, '<a href="$1" target="_blank">' )
		.replace(/\[\/url\]/gi, '</a>')
		.replace(/\[youtube](\w{11})\[\/youtube]/gi, '<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/$1?fs=1&amp;hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/$1?fs=1&amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>');
}


$(document).ready(function () {
    init();
});
